package com.leezk.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leezk.order.dto.TagMapperDto;
import com.leezk.order.entity.Dictionary;
import com.leezk.order.entity.Menu;
import com.leezk.order.entity.TagMapper;
import com.leezk.order.mapper.DictionaryMapper;
import com.leezk.order.mapper.MenuMapper;
import com.leezk.order.mapper.TagMapperMapper;
import com.leezk.order.service.ITagMapperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author leotong
 * @since 2022-03-16
 */
@Service
public class TagMapperServiceImpl extends ServiceImpl<TagMapperMapper, TagMapper> implements ITagMapperService {
    @Autowired
    private TagMapperMapper tagMapperMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public Page<TagMapperDto> getTagMappers(Page<TagMapper> page) {
        Page<TagMapper> tagMapperPage = tagMapperMapper.selectPage(page, null);
        Page<TagMapperDto> result=new Page<>();
        result.setTotal(tagMapperPage.getTotal());
        List<TagMapper> records = tagMapperPage.getRecords();
        List<TagMapperDto> tagMapperDtoList=new ArrayList<>();
        for (TagMapper record : records) {
            System.out.println(record);
            TagMapperDto mapperDto=new TagMapperDto();
            BeanUtils.copyProperties(record,mapperDto);
            List<Menu> menus=new ArrayList<>();
            if (record.getMid()!=null&& !Objects.equals(record.getMid(), "")){
                String[] split = record.getMid().split(",");
                for (String s : split) {
                    Menu menu = menuMapper.selectById(s);
                    if (menu!=null){
                        menus.add(menu);
                    }

                }
                mapperDto.setMenus(menus);
            }
            mapperDto.setDictionary(dictionaryMapper.selectById(record.getDid()));
            tagMapperDtoList.add(mapperDto);
        }
        result.setRecords(tagMapperDtoList);
        return result;
    }

}
